//
//  LYFDetailsViewController.m
//  段子手
//
//  Created by dllo on 16/3/18.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LYFDetailsViewController.h"

@interface LYFDetailsViewController ()<UITableViewDataSource, UITableViewDelegate, pushDelegate>

@property (nonatomic, retain) UITableView *tableView;/**< 详情tableview */
@property (nonatomic, retain) UIView *headerView;/**< tableview头视图 */
@property (nonatomic, retain) UIImageView *detailImg;/**< 头视图图片 */
@property (nonatomic, retain) UIButton *subscriptionButton;/**< 订阅头视图按钮 */
@property (nonatomic, retain) UILabel *peopleLabel;/**< 头视图人数 */
@property (nonatomic, retain) UILabel *articleLabel;/**< 头视图帖子数 */
@property (nonatomic, retain) UILabel *textLabel1;/**< 头视图文字 */
@property (nonatomic, retain) UIBarButtonItem *backButton;/**< 左上角返回按钮 */
@property (nonatomic ,retain) NSMutableArray *dataArr;/**< cell数据数组 */

@end

@implementation LYFDetailsViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.dataArr = [NSMutableArray array];
    }
    return self;
}


- (void)dealloc {
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui"] style:0 target:self action:@selector(backButtonAction:)];
    self.navigationItem.leftBarButtonItem = _backButton;
    self.navigationItem.title = _model.name;
    
    
    [self dataHandle];
    
    [self createHeaderView];
    [self createTableView];
    
    
    
}

/** 创建tableview */
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HETGHT) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[LYFDetailsTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    _tableView.tableHeaderView = _headerView;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LYFDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    LYFDetailsModel *model = _dataArr[indexPath.row];
    cell.model = model;
    cell.delegate = self;
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LYFDetailsModel *model = _dataArr[indexPath.row];
    
    NSDictionary *textDic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    CGRect rect = [model.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDic context:nil];
    
    return rect.size.height + 70 + 210 + 50;
}

/** 创建头视图 */
- (void)createHeaderView {
    

    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
    _headerView.backgroundColor = [UIColor colorWithRed:0.42 green:0.33 blue:0.27 alpha:1];
    self.detailImg = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 70, 70)];
    _detailImg.backgroundColor = [UIColor orangeColor];
    [_detailImg sd_setImageWithURL:[NSURL URLWithString:_model.icon_url]];
    [_headerView addSubview:_detailImg];
    
    self.subscriptionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _subscriptionButton.backgroundColor = [UIColor colorWithWhite:0.502 alpha:1.000];
    [_subscriptionButton setTitle:@"订阅" forState:UIControlStateNormal];
    _subscriptionButton.frame = CGRectMake(90, 10, 50, 30);
    _subscriptionButton.layer.cornerRadius = 6;
    [_headerView addSubview:_subscriptionButton];
    [_subscriptionButton addTarget:self action:@selector(subscriptionButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.peopleLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 50, 80, 30)];
//    _peopleLabel.backgroundColor = [UIColor whiteColor];
    _peopleLabel.text = [NSString stringWithFormat:@"%ld参与", _model.total_updates];
    _peopleLabel.textColor = [UIColor whiteColor];
    _peopleLabel.font = [UIFont systemFontOfSize:15];
    [_headerView addSubview:_peopleLabel];
    
    self.articleLabel = [[UILabel alloc] initWithFrame:CGRectMake(175, 50, 90, 30)];
//    _articleLabel.backgroundColor = [UIColor whiteColor];
    _articleLabel.text = [NSString stringWithFormat:@"%ld帖子", _model.subscribe_count];
    _articleLabel.textColor = [UIColor colorWithRed:1.000 green:0.502 blue:0.000 alpha:1.000];
    _articleLabel.font = [UIFont systemFontOfSize:15];
    [_headerView addSubview:_articleLabel];
    
    self.textLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 90, SCREEN_WIDTH - 20, 40)];
//    _textLabel1.backgroundColor = [UIColor whiteColor];
    _textLabel1.text = _model.placeholder;
    _textLabel1.textColor = [UIColor whiteColor];
    _textLabel1.font = [UIFont systemFontOfSize:15];
    [_headerView addSubview:_textLabel1];
    
    
}

- (void)pushComment:(LYFDetailsModel *)model {
    


    LYFCommentViewController *vc = [[LYFCommentViewController alloc] init];
    vc.model = model;
    
    [self.navigationController pushViewController:vc animated:YES];
}


/** 数据解析 */
- (void)dataHandle {
    
    NSString *str = [NSString stringWithFormat:@"http://ic.snssdk.com/neihan/stream/category/data/v2/?tag=joke&iid=3884185944&os_version=9.2.1&os_api=18&app_name=joke_essay&channel=App @tore&device_platform=iphone&idfa=F2C266F2-2A1C-4D42-85CC-07B17EE56F90&vid=68B1D0A2-5547-4C3F-B88A-D9B55797F72D&openudid=15d474a633f829aabda5b6e1420e0253418080e8&device_type=iPhone 6 Plus&version_code=5.0.1&ac=WIFI&screen_width=1242&device_id=12502863248&aid=7&category_id=%@&count=30&level=6&message_cursor=0&min_time=1458357870&mpic=1", _theId];
    
    [WxlAPPTools GETWithURL:str
                        par:nil success:^(id responseObject) {
        
        NSArray *arr = [LYFDetailsModel modelHandelWithArray:responseObject[@"data"][@"data"]];
        for (LYFDetailsModel *model in arr) {
            [_dataArr addObject:model];
        }
        
//        NSLog(@"%@", _dataArr);
        [_tableView reloadData];
        
        for (LYFDetailsModel *model in _dataArr) {
            NSLog(@"%@", model.bury_count);
        }
        
    } filed:^(NSError *error) {
        
    }];
}


/** 头视图订阅按钮点击方法 */
- (void)subscriptionButtonAction:(UIButton *) button {
    
    [[LYFDataBaseHandlle sharedDataBaseHandle] openDB];
    [[LYFDataBaseHandlle sharedDataBaseHandle] createTable];
    
    NSMutableArray *arrName = [NSMutableArray array];
    NSMutableArray *arrModel = [[LYFDataBaseHandlle sharedDataBaseHandle] selectTable];
    for (LYFDiscoverModel *model in arrModel) {
        [arrName addObject:model.name];
    }
    
    if ([arrName containsObject:_model.name]) {
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"该专题已被订阅" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];
        
        
    } else {
        
        [[LYFDataBaseHandlle sharedDataBaseHandle] insertModel:_model];
        
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"订阅成功" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"确定" Click:^{
            
        }];
        
    }

}

/** 左上角返回按钮点击方法 */
- (void)backButtonAction:(UIBarButtonItem *) backButton {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
